from datetime import datetime

from django.contrib.auth import authenticate, login,logout
from django.contrib.auth.models import User
from django.core.paginator import Paginator
from django.http import JsonResponse, HttpResponse, request, HttpResponseRedirect
from django.shortcuts import render, redirect, reverse
from .models import *
from .forms import PostForm  # 后续创建表单类
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
# Create your views here.
def index(request):
    contents = ContentTable.objects.filter(fenlei_id=1)

    print(locals())
    return render(request, 'index.html', locals())


def natural(request):
    contents = ContentTable.objects.filter(fenlei_id=2)
    print(locals())
    return render(request, 'natural.html', locals())


def show_food(request):
    contents = ContentTable.objects.filter(fenlei_id=3)

    print(locals())
    return render(request, "food.html", locals())


def history(request):
    contents = ContentTable.objects.filter(fenlei_id=4)
    print(locals())
    return render(request, "history.html", locals())


def minsufengqing(request):
    contents = ContentTable.objects.filter(fenlei_id=5)
    print(locals())
    return render(request, "minsufengqing.html.", locals())




@login_required
def changyanyunge (request):
    # 1. 处理发帖逻辑：如果是 POST 请求，保存数据到数据库
    if request.method == 'POST':
        title = request.POST.get('title')   # 获取表单里的标题
        content = request.POST.get('content')  # 获取表单里的内容
        # 创建帖子并保存到数据库
        Post.objects.create(title=title, content=content,uid=request.user)
        # 发布成功后，重定向到自身，避免刷新重复提交
        return redirect('/changyanyunge/')

    # 2. 读取所有帖子，按发布时间倒序
    posts = Post.objects.all().order_by('-comment_time')
    # 3. 把帖子数据传给模板
    # return render(request, 'post_list.html', {'posts': posts})
    return render(request, 'changyanyunge.html',{'posts': posts})


def register(request):
    if request.method == 'POST':
        try:
            username = request.POST.get('username')
            password = request.POST.get('password')
            email = request.POST.get('email')
            # 验证用户是否存在
            user = User.objects.filter(username=username).first()
            print(username,password,email)
            print(user)
            if user:
                # 用户已经存在
                return render(request, 'register.html', {'msg': '用户名已存在'})
            else:
                # 保存用户
                User.objects.create_user(username=username, password=password, email=email)
                return redirect(reverse("app:login"))
        except Exception as e:
            return render(request, 'register.html', {'msg': '注册失败'})
    else:
        return render(request, 'register.html')


def login_denglu(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        # 验证用户是否存在
        user = authenticate(request, username=username, password=password)
        if user:
            login(request, user)
            return redirect('/changyanyunge/')
        else:
            return render(request, 'login.html', {'msg': '用户密码错误'})

    else:
        return render(request, 'login.html')

def guanyuwomen(request):
    contents = ContentTable.objects.filter(fenlei_id=7)
    print(locals())
    return render(request, "guanyuwomen.html", locals())


def lianxiwomen(request):
    contents = ContentTable.objects.filter(fenlei_id=8)
    print(locals())
    return render(request, "lianxiwomen.html", locals())

def authenticate_user(request):
    pwd = request.POST.get("pwd", "")
    u_name = request.POST.get("u_name", "")
    if len(pwd) <= 0 or len(u_name) <= 0:
        return HttpResponse("账号或密码不能为空")
    else:
        user = authenticate(username=u_name, password=pwd)
        if user:
            return HttpResponse("验证成功")
        else:
            return HttpResponse("账号或密码错误")





def article_detail(request):


        return render(request, "articles2.html", locals())


def article2_detail(request):
     return render(request, "articles.html", locals())


def article3_detail(request):
    return render(request, "articles3.html", locals())


def article4_detail(request):
    return render(request, "articles4.html", locals())







def article5_detail(request):
    return render(request, "articles5.html", locals())


def article6_detail(request):
    return render(request, "articles6.html", locals())


def article7_detail(request):
    return render(request, "articles7.html", locals())


def user_logout(request):
    logout(request)
    return redirect("/")